package com.yzjs.localhost.service.hrmContract.impl;

import com.alibaba.fastjson.JSONObject;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmContract.HrmContractEntity;
import com.yzjs.localhost.dao.hrmContract.HrmContractMapper;
import com.yzjs.localhost.entity.sysMenu.SysMenuEntity;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmContract.HrmContractService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("hrmContractService")
public class HrmContractServiceImpl extends BaseServiceImpl<HrmContractEntity, HrmContractMapper > implements HrmContractService {

    @Override
    public ComparisonResultDTO hrmContractCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity beforce =dto.getBeforce();
        String beforceInfo  = beforce.getInfo();
        Map<String,Object> beforceParse = JsonUtil.json2map(beforceInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo  = after.getInfo();

        ComparisonResultDTO resultDTO = null ;
        if (beforce.getOperatorCode().contains("add")){
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 菜单图标  显示顺序 菜单名称 路由地址 菜单类型
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"endDate,signingDate,conName,remark,delFlag,conContent,conStatus,conCode,createBy,conType,conDeptId,conUserId,startDate");
        }else if (beforce.getOperatorCode().contains("edit")){
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 菜单ID  菜单图标 显示顺序  菜单名称  路由地址 菜单类型
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"endDate,signingDate,conName,delFlag,conContent,updateBy,createDate,conStatus,,conCode,conDeptName,createBy,conType,conDeptId,contractId,conUserId,conUserName,startDate");
        } else if (beforce.getOperatorCode().contains("remove")){
            beforceParse.put("delFlag","2");
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 菜单ID 状态
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"contractIds,delFlag");
            if (null==afterParse.get("contractIds")){
                resultDTO.setAbstractName("合同ID为空");
                resultDTO.setWarningInfo("合同ID为空");
                resultDTO.setMathFlag(false);
                return resultDTO;
            }
            HrmContractEntity hrmContractEntity = new HrmContractEntity();
            hrmContractEntity.setContractId(Long.valueOf((String) afterParse.get("contractIds")));
            hrmContractEntity.setDelFlag((String) afterParse.get("delFlag"));
            hrmContractEntity.setDataIdentification(after.getDataIdentification());
            this.insert(hrmContractEntity);
            return resultDTO;
        }
        // 记录表表信息
        HrmContractEntity hrmContractEntity = JSONObject.parseObject(after.getInfo(), HrmContractEntity.class);
        hrmContractEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        hrmContractEntity.setDataIdentification(after.getDataIdentification());
        this.insert(hrmContractEntity);

        return resultDTO;
    }

}